﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class ManageQuestionBank : System.Web.UI.Page
{
    DataTable dt;
    static ArrayList arrOptionsName = new ArrayList();
    static ArrayList arrOptionId = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadSubject();
            //bindGrid();
        }
    }

    private void bindGrid()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetQuestionInfo(Convert.ToInt32(ddlSubject.SelectedValue),Convert.ToInt32(ddlTopics.SelectedValue));
        dt.Columns.Add("IsMul", typeof(System.String));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][4].ToString().Contains('|'))
                dt.Rows[i][4] = dt.Rows[i][4].ToString().Replace('|', ',');
        }


        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][7].ToString() == "1")
                dt.Rows[i]["IsMul"] = "Yes";
            else
                dt.Rows[i]["IsMul"] = "No";
        }

        dt.Columns.Add("IsAct", typeof(System.String));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][8].ToString() == "1")
                dt.Rows[i]["IsAct"] = "Yes";
            else
                dt.Rows[i]["IsAct"] = "No";
        }
        gvQuestion.DataSource = dt;
        gvQuestion.DataBind();
    }

    private void LoadSubject()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        ddlSub.Items.Add("--Select One--");
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("--Select One--"));
        ddlSubject.Items.Add("--Select One--");
        ddlSubject.SelectedIndex = ddlSubject.Items.IndexOf(ddlSubject.Items.FindByText("--Select One--"));
        ListItem list = new ListItem();
        list.Text = "All";
        list.Value = "0";
        ddlSubject.Items.Add(list);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
            ddlSubject.Items.Add(li);
        }
    }

    private void LoadTopic(int subjectId)
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.getTopicRelatedToSubject(subjectId);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlTopic.Items.Add(li);
        }
    }

    protected void btnAddEdit_Click(object sender, EventArgs e)
    {
        if (lblOptions.SelectedIndex == -1)
        {
            ListItem li = new ListItem();
            if (txtAddEditOptions.Text == "")
            {
                lablError.Text = "Please Enter New Topic";
                txtAddEditOptions.Focus();
            }

            else
            {
                rdoOptionList.Items.Add(new ListItem(txtAddEditOptions.Text));
                chkOptionList.Items.Add(new ListItem(txtAddEditOptions.Text));
                arrOptionsName.Add(txtAddEditOptions.Text);
                li.Text = txtAddEditOptions.Text;
                li.Value = txtAddEditOptions.Text;
                lblOptions.Items.Add(li);
                txtAddEditOptions.Text = "";
            }
        }
        else
        {
            if (gvQuestion.SelectedIndex == -1)
            {
                arrOptionsName.Remove(lblOptions.SelectedItem.Text);
                lblOptions.Items.Remove(lblOptions.SelectedItem.Text);
                arrOptionsName.Add(txtAddEditOptions.Text);
                ListItem li = new ListItem();
                li.Text = txtAddEditOptions.Text;
                li.Value = txtAddEditOptions.Text;
                lblOptions.Items.Add(li);
                rdoOptionList.Items.Add(new ListItem(txtAddEditOptions.Text));
                chkOptionList.Items.Add(new ListItem(txtAddEditOptions.Text));
                txtAddEditOptions.Text = "";
            }
            else
            {
                lblOptions.Items.RemoveAt(lblOptions.SelectedIndex);
                arrOptionsName.Add(txtAddEditOptions.Text);
                arrOptionId.Add(txtAddEditOptions.ToolTip);
                ListItem li = new ListItem();
                li.Text = txtAddEditOptions.Text;
                li.Value = txtAddEditOptions.ToolTip;
                lblOptions.Items.Add(li);
                for (int i = 0; i < rdoOptionList.Items.Count; i++)
                {
                    if (rdoOptionList.Items[i].Value == txtAddEditOptions.ToolTip)
                        rdoOptionList.Items[i].Text = txtAddEditOptions.Text;
                }
                for (int i = 0; i < chkOptionList.Items.Count; i++)
                {
                    if (chkOptionList.Items[i].Value == txtAddEditOptions.ToolTip)
                        chkOptionList.Items[i].Text = txtAddEditOptions.Text;
                }
                txtAddEditOptions.Text = "";
            }
        }
    }

    private void clearAll()
    {
        ddlSub.SelectedIndex = -1;
        ddlTopic.Items.Clear();
        rdoOptionList.Items.Clear();
        chkOptionList.Items.Clear();
        chkEnableMul.Checked = false;
        chkOptionList.Visible = false;
        rdoOptionList.Visible = true;
        ddlPriority.SelectedIndex = -1;
        txtAddTopic.Text = "";
        txtQuestion.Text = "";
        txtAddEditOptions.Text = "";
        txtJustification.Text = "";
        lblOptions.Items.Clear();
        btnAddEditOption.Text = "Add";
        //txtAddTopic.Enabled = false;
        arrOptionsName.Clear();
        arrOptionId.Clear();
        gvQuestion.SelectedIndex = -1;
        chkIsActive.Checked = false;
        lablError.Text = "";
        
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ListItem li = new ListItem();
        if (txtAddTopic.Text == "")
        {
            lablError.Text = "Please Enter New Topic";
            txtAddEditOptions.Focus();
        }
        else
        {
            dt = new DataTable();
            li.Text = txtAddTopic.Text;
            ddlTopic.Items.Add(li);
            ddlTopic.SelectedIndex = ddlTopic.Items.IndexOf(ddlTopic.Items.FindByText(li.Text));
            dt = AdminCreateTestDAL.setTopic(Convert.ToInt32(ddlSub.SelectedItem.Value), txtAddTopic.Text);
            ddlTopic.SelectedItem.Value = dt.Rows[0][0].ToString();
            txtAddTopic.Text = "";
        }
    }

    protected void lblOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnAddEditOption.Text = "Edit";
        btnAddEditOption.Enabled = true;
        txtAddEditOptions.Text = lblOptions.SelectedItem.Text;
        txtAddEditOptions.ToolTip = lblOptions.SelectedItem.Value;
        if (gvQuestion.SelectedIndex == -1)
        {
            rdoOptionList.Items.Remove(lblOptions.SelectedItem.Text);
            chkOptionList.Items.Remove(lblOptions.SelectedItem.Text);
        }
    }

    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAddTopic.Enabled = true;
        ddlTopic.Items.Clear();
        if (ddlSub.SelectedItem.Text == "--Select One--")
        {
            lablError.Text = "Please select one subject";
            
        }
        else
        {
            dt = new DataTable();
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                ddlTopic.Items.Add(li);
            }
        }
    }

    private string getAnswers()
    {
        string ans = "";
        if (chkEnableMul.Checked == true)
        {
            if (chkOptionList.SelectedIndex == -1)
            {
                lablError.Text = "Please select at least one answer";
                //txtAddEditOptions.Focus();
            }
            else
            {
                for (int i = 0; i < chkOptionList.Items.Count; i++)
                {
                    if (chkOptionList.Items[i].Selected)
                        ans += chkOptionList.Items[i].Text + "|";
                }
            }
        }
        else
        {
            if (rdoOptionList.SelectedIndex == -1)
                lablError.Text = "Please select at least one answer";
            else
                ans = rdoOptionList.SelectedItem.Text;
        }
        return ans;
    }

    private string getOptions()
    {
        string option = "";
        for (int i = 0; i < chkOptionList.Items.Count; i++)
        {
            option += chkOptionList.Items[i].Text + "|";
        }
        return option;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string option = getOptions();
        string answer = getAnswers();
        string optionId = "";
        int multiple = -1;
        if (chkEnableMul.Checked == true)
            multiple = 1;
        else
            multiple = 0;
        int isActive;
        if (chkIsActive.Checked == true)
            isActive = 1;
        else
            isActive = 0;
        if (gvQuestion.SelectedIndex == -1)
        {
            AdminCreateTestDAL.ManngeQuestion(Convert.ToInt32(ddlSub.SelectedItem.Value), Convert.ToInt32(ddlTopic.SelectedItem.Value), txtQuestion.Text, option, answer, txtJustification.Text, ddlPriority.SelectedItem.Text, multiple, isActive);
            clearAll();
            lablError.Text = "Question inserted Successfully";
        }
        else
        {
            string allOptionName = "";
            string allOptionId = "";

            for (int i = 0; i < arrOptionId.Count; i++)
                allOptionId += arrOptionId[i].ToString() + "|";

            for (int i = 0; i < arrOptionsName.Count; i++)
                allOptionName += arrOptionsName[i].ToString() + "|";

            AdminCreateTestDAL.ManngeQuestionUpdation(Convert.ToInt32(ddlSub.SelectedItem.Value), Convert.ToInt32(ddlTopic.SelectedItem.Value), txtQuestion.Text, allOptionName, allOptionId, answer, txtJustification.Text, ddlPriority.SelectedItem.Text, multiple, Convert.ToInt32(gvQuestion.SelectedDataKey.Value), isActive);
            clearAll();
            lablError.Text = "Question updated successfully";
        }

        
        //bindGrid();
        btnAddEditOption.Text = "Add";
        arrOptionsName.Clear();
        arrOptionId.Clear();
    }

    protected void chkEnableMul_CheckedChanged(object sender, EventArgs e)
    {
        if (chkEnableMul.Checked == true)
        {
            rdoOptionList.Visible = false;
            chkOptionList.Visible = true;
        }
        else
        {
            rdoOptionList.Visible = true;
            chkOptionList.Visible = false;
        }
    }

    protected void gvQuestion_SelectedIndexChanged(object sender, EventArgs e)
    {
        arrOptionsName.Clear();
        arrOptionId.Clear();
        lblOptions.Items.Clear();
        btnAddEditOption.Enabled = false;
        chkEnableMul.Enabled = true;
        int id = Convert.ToInt32(gvQuestion.SelectedDataKey.Value);
        DataTable MyDt = new DataTable();
        MyDt = AdminCreateTestDAL.GetQuestionInfoById(id);
        ddlSub.Items.Clear();
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
        //LoadSubject();
        //ddlSub.SelectedIndex = -1;
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByValue(MyDt.Rows[0]["SubjectId"].ToString()));
        ddlTopic.Items.Clear();
        LoadTopic(Convert.ToInt32(MyDt.Rows[0]["SubjectId"].ToString()));
        //ddlTopic.SelectedIndex = -1;
        ddlTopic.SelectedIndex = ddlTopic.Items.IndexOf(ddlTopic.Items.FindByValue(MyDt.Rows[0]["topicId"].ToString()));
        txtQuestion.Text = MyDt.Rows[0][2].ToString();
        txtJustification.Text = MyDt.Rows[0]["Justification"].ToString();
        ddlPriority.Items.Add(new ListItem("Low"));
        ddlPriority.Items.Add(new ListItem("Medium"));
        ddlPriority.Items.Add(new ListItem("High"));
        ddlPriority.SelectedIndex = ddlPriority.Items.IndexOf(ddlPriority.Items.FindByText(MyDt.Rows[0]["priority"].ToString()));
        if (MyDt.Rows[0]["IsMultiple"].ToString() == "1")
            chkEnableMul.Checked = true;
        else
            chkEnableMul.Enabled = false;
        rdoOptionList.Items.Clear();
        chkOptionList.Items.Clear();
        for (int i = 0; i < MyDt.Rows.Count; i++)
        {
            lblOptions.Items.Add(new ListItem(MyDt.Rows[i]["Options"].ToString(), MyDt.Rows[i]["OptionId"].ToString()));
            rdoOptionList.Items.Add(new ListItem(MyDt.Rows[i]["Options"].ToString(), MyDt.Rows[i]["OptionId"].ToString()));
            chkOptionList.Items.Add(new ListItem(MyDt.Rows[i]["Options"].ToString(), MyDt.Rows[i]["OptionId"].ToString()));
        }
        if (chkEnableMul.Checked == true)
        {
            rdoOptionList.Visible = false;
            chkOptionList.Visible = true;
            string ans = MyDt.Rows[0]["Answer"].ToString();
            string[] answers = ans.Split('|');
            for (int i = 0; i < chkOptionList.Items.Count; i++)
            {
                for (int j = 0; j < answers.Length; j++)
                {
                    if (chkOptionList.Items[i].Text == answers[j])
                        chkOptionList.Items[i].Selected = true;

                }
            }

        }
        else
        {
            rdoOptionList.Visible = true;
            chkOptionList.Visible = false;
            rdoOptionList.SelectedIndex = rdoOptionList.Items.IndexOf(rdoOptionList.Items.FindByText(MyDt.Rows[0]["Answer"].ToString()));
        }

        if (MyDt.Rows[0]["IsActive"].ToString() == "1")
            chkIsActive.Checked = true;
        else
            chkIsActive.Checked = false;
    }

    protected void gvQuestion_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvQuestion.PageIndex = e.NewPageIndex;
        bindGrid();
        gvQuestion.DataBind();
    }


    protected void ddlSubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlTopics.Items.Clear();
        if (ddlSubject.SelectedItem.Text == "--Select One--")
        {
            lablError.Text = "Please select one subject";

        }
        else
        {
            dt = new DataTable();
            ListItem list = new ListItem();
            list.Text = "All";
            list.Value = "0";
            ddlTopics.Items.Add(list);
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSubject.SelectedItem.Value));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                ddlTopics.Items.Add(li);
            }
        }
    }
    protected void ddlTopics_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    protected void btnShow_Click(object sender, EventArgs e)
    {
        lablAvailQues.Visible = true;
        bindGrid();
    }
    protected void txtAddTopic_TextChanged(object sender, EventArgs e)
    {
        lablError.Text = "";
    }
    protected void btnClearSearch_Click(object sender, EventArgs e)
    {
        lablAvailQues.Visible = false;
        gvQuestion.DataSource = null;
        gvQuestion.DataBind();
    }
}